import 'dart:math' as math;
import 'package:flutter/material.dart';

class AnimatedBackground extends StatefulWidget {
  final Widget child;

  const AnimatedBackground({Key? key, required this.child}) : super(key: key);

  @override
  State<AnimatedBackground> createState() => _AnimatedBackgroundState();
}

class _AnimatedBackgroundState extends State<AnimatedBackground>
    with TickerProviderStateMixin {
  late final List<AnimationController> _controllers;
  late final List<Animation<double>> _animations;
  final List<Bubble> _bubbles = [];
  final int _numBubbles = 8;

  @override
  void initState() {
    super.initState();
    _controllers = List.generate(
      _numBubbles,
      (index) => AnimationController(
        duration: Duration(seconds: 10 + index * 2),
        vsync: this,
      ),
    );

    _animations =
        _controllers.map((controller) {
          return Tween<double>(begin: 0, end: 1).animate(
            CurvedAnimation(parent: controller, curve: Curves.easeInOut),
          );
        }).toList();

    _createBubbles();
    _startAnimations();
  }

  void _createBubbles() {
    final random = math.Random();
    final colors = [
      Colors.blue.withOpacity(0.15),
      Colors.purple.withOpacity(0.15),
      Colors.teal.withOpacity(0.15),
      Colors.pink.withOpacity(0.15),
      Colors.indigo.withOpacity(0.15),
      Colors.cyan.withOpacity(0.15),
    ];

    for (int i = 0; i < _numBubbles; i++) {
      final gradientColors = [
        colors[random.nextInt(colors.length)],
        colors[random.nextInt(colors.length)],
      ];

      _bubbles.add(
        Bubble(
          size: 150 + random.nextDouble() * 250,
          initialPosition: Offset(
            random.nextDouble() * 1200 - 600,
            random.nextDouble() * 1200 - 600,
          ),
          finalPosition: Offset(
            random.nextDouble() * 1200 - 600,
            random.nextDouble() * 1200 - 600,
          ),
          gradientColors: gradientColors,
          rotation: random.nextDouble() * math.pi * 2,
        ),
      );
    }
  }

  void _startAnimations() {
    for (var controller in _controllers) {
      controller.repeat(reverse: true);
    }
  }

  @override
  void dispose() {
    for (var controller in _controllers) {
      controller.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      fit: StackFit.expand,
      children: [
        Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Theme.of(context).colorScheme.primary.withOpacity(0.05),
                Theme.of(context).colorScheme.primary.withOpacity(0.1),
              ],
            ),
          ),
        ),
        ...List.generate(_numBubbles, (index) {
          return AnimatedBuilder(
            animation: _animations[index],
            builder: (context, child) {
              final bubble = _bubbles[index];
              final position =
                  Offset.lerp(
                    bubble.initialPosition,
                    bubble.finalPosition,
                    _animations[index].value,
                  )!;

              return Positioned(
                left: MediaQuery.of(context).size.width / 2 + position.dx,
                top: MediaQuery.of(context).size.height / 2 + position.dy,
                child: Transform.rotate(
                  angle:
                      bubble.rotation + _animations[index].value * math.pi * 2,
                  child: Transform.scale(
                    scale:
                        math.sin(_animations[index].value * math.pi) * 0.3 +
                        0.7,
                    child: Container(
                      width: bubble.size,
                      height: bubble.size,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        gradient: LinearGradient(
                          colors: bubble.gradientColors,
                          begin: Alignment.topLeft,
                          end: Alignment.bottomRight,
                        ),
                      ),
                    ),
                  ),
                ),
              );
            },
          );
        }),
        widget.child,
      ],
    );
  }
}

class Bubble {
  final double size;
  final Offset initialPosition;
  final Offset finalPosition;
  final List<Color> gradientColors;
  final double rotation;

  Bubble({
    required this.size,
    required this.initialPosition,
    required this.finalPosition,
    required this.gradientColors,
    required this.rotation,
  });
}
